package com.ruoyi.house_sale.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.house_sale.domain.PaidScheme;
import com.ruoyi.house_sale.domain.Schemeflow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.house_sale.mapper.HouseinfoMapper;
import com.ruoyi.house_sale.domain.Houseinfo;
import com.ruoyi.house_sale.service.IHouseinfoService;

import javax.annotation.Resource;
import javax.validation.Validator;

/**
 * 房产资料Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-29
 */
@Service
public class HouseinfoServiceImpl implements IHouseinfoService 
{
    private static final Logger log = LoggerFactory.getLogger(SchemeflowServiceImpl.class);

    @Resource
    private HouseinfoMapper houseinfoMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询房产资料
     * 
     * @param hourseid 房产资料主键
     * @return 房产资料
     */
    @Override
    public Houseinfo selectHouseinfoByHourseid(Long hourseid)
    {
        return houseinfoMapper.selectHouseinfoByHourseid(hourseid);
    }

    /**
     * 查询房产资料列表
     * 
     * @param houseinfo 房产资料
     * @return 房产资料
     */
    @Override
    public List<Houseinfo> selectHouseinfoList(Houseinfo houseinfo)
    {
        return houseinfoMapper.selectHouseinfoList(houseinfo);
    }

    /**
     * 新增房产资料
     * 
     * @param houseinfo 房产资料
     * @return 结果
     */
    @Override
    public int insertHouseinfo(Houseinfo houseinfo)
    {
        return houseinfoMapper.insertHouseinfo(houseinfo);
    }

    /**
     * 修改房产资料
     * 
     * @param houseinfo 房产资料
     * @return 结果
     */
    @Override
    public int updateHouseinfo(Houseinfo houseinfo)
    {
        return houseinfoMapper.updateHouseinfo(houseinfo);
    }

    /**
     * 批量删除房产资料
     * 
     * @param hourseids 需要删除的房产资料主键
     * @return 结果
     */
    @Override
    public int deleteHouseinfoByHourseids(Long[] hourseids)
    {
        return houseinfoMapper.deleteHouseinfoByHourseids(hourseids);
    }

    /**
     * 删除房产资料信息
     * 
     * @param hourseid 房产资料主键
     * @return 结果
     */
    @Override
    public int deleteHouseinfoByHourseid(Long hourseid)
    {
        return houseinfoMapper.deleteHouseinfoByHourseid(hourseid);
    }

    @Override
    public int updatecommisionpaid(Houseinfo houseinfo) {
        return houseinfoMapper.updatecommisionpaid(houseinfo);
    }

    @Override
    public int updisbalance(Long hourseid) {
        return houseinfoMapper.updisbalance(hourseid);
    }

    /**
     * 导入房产资料数据
     *
     * @param houseinfoList 房产资料数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    @Override
    public String importHouseinfo(List<Houseinfo> houseinfoList, Boolean isUpdateSupport) {
        if (StringUtils.isNull(houseinfoList) || houseinfoList.size() == 0)
        {
            throw new ServiceException("导入房产资料数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Houseinfo houseinfo : houseinfoList)
        {
            try
            {
                // 验证是否存在这个方案流程
                Houseinfo h = houseinfoMapper.selectHouseinfoByHourseid(houseinfo.getHourseid());
                if (!StringUtils.isNull(h) && isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, houseinfo);
                    houseinfo.setSalestate("");
                    this.updateHouseinfo(houseinfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、房产编号 " + houseinfo.getHourseid() + " 的房产导入并修改成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、房产编号 " + houseinfo.getHourseid() + " 的房产不存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、房产编号 " + houseinfo.getHourseid() + " 的房产导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
